<?php

namespace Bigspender\DefaultBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\DBAL\Connection;

/**
* @Annotation
*/
class EmailNotTakenValidator extends ConstraintValidator
{
	private $connection;

	public function __construct($dbalConnection)  {
		$this->connection = $dbalConnection;
	}

	public function validate($value, Constraint $constraint)
	{
		$sql = 'SELECT * FROM "user" WHERE email = ?';
		$statement = $this->connection->prepare($sql);
		$statement->bindValue(1, $value);
		$statement->execute();
		$users = $statement->fetchAll();

		if (count($users)) {
			$this->context->addViolation($constraint->message, array());
		}
	}
}
